package org.example.smartbuy.controller;

import org.example.smartbuy.Result;
import org.example.smartbuy.entity.Products;
import org.example.smartbuy.service.IProductsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/products")
@Tag(name = "Products Controller", description = "管理员进行增删改操作")
public class ProductsController {

    @Autowired
    private IProductsService productsService;

    /**
     * 获取所有产品
     * @return List<Products>
     */
    @GetMapping("/list")
    @Operation(summary = "Get all products", description = "Fetch a list of all products",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result listAllProducts() {
        return Result.success(productsService.listAll());
    }

    /**
     * 添加新产品
     * @param products 产品对象
     * @return Products
     */
    @PostMapping("/add")
    @Operation(summary = "Add a new product", description = "新增商品的同时新增库存",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result addProduct(@RequestBody Products products, int amount, int adminId) {
        return Result.success(productsService.add(products, amount, adminId));
    }

    /**
     * 更新产品信息
     * @param products 更新的产品信息
     * @return Products
     */
    @PutMapping("/update")
    @Operation(summary = "Update a product", description = "Update an existing product",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result updateProduct(@RequestBody Products products, int adminId) {
        return Result.success(productsService.update(products, adminId));
    }

    /**
     * 删除产品
     * @param id 产品ID
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "Delete a product", description = "Delete a product by its ID",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result deleteProduct(@PathVariable int id, int adminId) {
        productsService.delete(id, adminId);
        return  Result.success();
    }
}
